package com.superjson.showpro.service.impl;

import com.superjson.showpro.base.GenericServiceImpl;
import com.beantools.util.FastDateFormat;
import com.beantools.util.ObjectUtil;
import com.superjson.showpro.dao.SystemRecordDao;
import com.superjson.showpro.entity.SystemRecord;
import com.superjson.showpro.entity.User;
import com.superjson.showpro.service.SystemRecordService;
import com.superjson.showpro.shared.result.Result;
import com.superjson.showpro.shared.result.ResultUtil;
import com.superjson.showpro.shared.util.IpUtill;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.UserAgent;
import nl.bitwalker.useragentutils.Version;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

import static com.beantools.constants.SingleChar.NULL_OP;
import static com.beantools.entity.enums.DateFormatEnum.ISO_DATETIME_FORMAT;


@Service
public class SystemRecordServiceImpl extends GenericServiceImpl<SystemRecord> implements SystemRecordService {

    private SystemRecordDao systemRecordDao;

    @Autowired
    public SystemRecordServiceImpl(SystemRecordDao systemRecordDao) {
        super(systemRecordDao);
        this.systemRecordDao = systemRecordDao;
    }

    @Override
    public Result saveSystem(User user, String ua, HttpServletRequest request) {
        SystemRecord systemRecord = new SystemRecord();
        systemRecord.setIp(IpUtill.getIpAddr(request));
        if (ObjectUtil.isNotNull(user)) {
            systemRecord.setUser(user);
        }
        UserAgent userAgent = UserAgent.parseUserAgentString(ua);
        Browser browser = userAgent.getBrowser();
        systemRecord.setBrowserName(browser.getName());
        systemRecord.setBrowserType(browser.getBrowserType());
        Version version = browser.getVersion(ua);
        systemRecord.setBrowserVersion(version.getVersion());
        systemRecord.setOperatingSystem(userAgent.getOperatingSystem());
        systemRecordDao.save(systemRecord);
        return ResultUtil.buildSuccess();
    }

    @Override
    public Result<Long> countTodayPageView() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MILLISECOND,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        String startTime= FastDateFormat.formatByEnum(calendar.getTime(), ISO_DATETIME_FORMAT);
        calendar.set(Calendar.HOUR_OF_DAY,24);
        String endTime=FastDateFormat.formatByEnum(calendar.getTime(),ISO_DATETIME_FORMAT);
        Long todayPageView = systemRecordDao.countPageView(startTime,endTime );
        return ResultUtil.buildSuccess(todayPageView);
    }


    @Override
    public Result<Long> countAllPageView() {
        Long allPageView = systemRecordDao.countPageView(NULL_OP, NULL_OP);
        return ResultUtil.buildSuccess(allPageView);
    }
}